package me.ezjs.framework.core.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询条件节点.
 * Created by zero-mac on 16/3/10.
 */
public class ParamNode {

    /**
     * 关联关系
     */
    public enum Relation {
        and(" and "),//
        or(" or "),//
        ;

        String sql;
        Relation(String sql) {
            this.sql = sql;
        }

        public String getSql() {
            return sql;
        }
    }

    //创建父子节点
    public ParamNode(Relation relation) {
        this.relation = relation;
        this.nodes = new ArrayList<>();
    }

    /**
     * 创建父子节点
     *
     * @param relation 关联关系
     * @param nodes    节点
     */
    public ParamNode(Relation relation, ParamNode... nodes) {
        this.relation = relation;
        this.nodes = new ArrayList<>();
        this.addArrayNodes(nodes);
    }

    public ParamNode(Relation relation, Parameter... params) {
        this.relation = relation;
        this.nodes = new ArrayList<>();
        this.addArrayParams(params);
    }


    public ParamNode addNodes(ParamNode... nodes) {
        this.addArrayNodes(nodes);
        return this;
    }

    /**
     * 创建叶子节点
     *
     * @param parameter
     */
    public ParamNode(Parameter parameter) {
        this.parameter = parameter;
    }

    private Relation relation;//不为空表示非叶子节点

    private Parameter parameter;//不为空表明已经是叶子节点

    private List<ParamNode> nodes;//子节点

    public Relation getRelation() {
        return relation;
    }

    public void setRelation(Relation relation) {
        this.relation = relation;
    }

    public Parameter getParameter() {
        return parameter;
    }

    public void setParameter(Parameter parameter) {
        this.parameter = parameter;
    }

    public List<ParamNode> getNodes() {
        return nodes;
    }

    public void setNodes(List<ParamNode> nodes) {
        this.nodes = nodes;
    }

    private void addArrayNodes(ParamNode... nodes) {
        for (ParamNode node :
                nodes) {
            this.nodes.add(node);
        }
    }

    private void addArrayParams(Parameter... params) {
        for (Parameter p :
                params) {
            this.nodes.add(new ParamNode(p));
        }
    }

}
